#include <head.h>
#include "emp.h"
//员工管理系统客户端

//管理员操作函数
void do_admim(int sockfd, NETDATA_t *msg);
//用户操作函数
void do_user(int sockfd, NETDATA_t *msg);
//登陆函数
int send_login(int sockfd, NETDATA_t *msg);
//查询函数
void send_search(int sockfd, NETDATA_t *msg);
//修改函数
void send_modify(int sockfd, NETDATA_t *msg);
//新增员工函数
void send_add(int sockfd, NETDATA_t *msg);
//删除员工函数
void send_del(int sockfd, NETDATA_t *msg);
//退出操作函数
void do_quit(int sockfd, NETDATA_t *msg);
//管理员查询菜单函数
void admin_checkmenu()
{
    printf("**************!!!热烈欢迎lrx管理员!!!************\n");
    printf("*****************使用员工管理系统****************\n");
    printf("1.查寻员工信息    2.增加新员工信息   3.修改员工信息\n");
    printf("4.删除员工信息    5.退出\n");
    printf("************************************************\n");
    printf("Input->>");
}

//用户查询菜单函数
void user_checkmenu()
{
    printf("*******欢迎使用lrx的员工管理系统*******\n");
    printf("1.查寻本人信息   2.修改密码    3.退出\n");
    printf("*************************************\n");
    printf("Input->>");
}

//登录注册菜单函数
void print_menu()
{
    printf("*******欢迎使用lrx的员工管理系统*******\n");
    printf("       1.登录           2.退出       \n");
    printf("*************************************\n");
    printf("Input->>");
}
//主函数
int main(int argc, const char *argv[])
{
    //校验参数
    if (3 != argc)
    {
        printf("Input error,try again!");
        printf("Usage:%s <IP> <port>", argv[0]);
        exit(-1);
    }
    int sockfd = 0;
    int ret;
    char choose[2];

    // 1.创建套接字
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (-1 == sockfd)
        ERRLOG("sockfd error\n");

    // 2.填充网络信息结构体
    struct sockaddr_in serveraddr;
    serveraddr.sin_family = AF_INET;                 // IPV4
    serveraddr.sin_port = htons(atoi(argv[2]));      // port
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]); // ip
    socklen_t serveraddr_len = sizeof(serveraddr);   //网络信息结构体大小

    // 3.连接至服务器
    if (-1 == (ret = connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)))
        ERRLOG("connect error\n");

    printf("已连接至服务器\n");

    NETDATA_t msg;
    // 操作
    while (1)
    {
        bzero(&msg, sizeof(NETDATA_t));
        print_menu();
        fgets(choose, sizeof(choose), stdin);
        getchar();
        switch (choose[0])
        {
        case '1':
            //发送登录请求
            ret = send_login(sockfd, &msg);
            break;
        case '2':
            //退出
            do_quit(sockfd, &msg);
            close(sockfd);
            return 0;
            break;
        default:
            break;
        }

        if (2 == ret)
        {
            do_admim(sockfd, &msg);
            break;
        }
        else if (1 == ret)
        {
            do_user(sockfd, &msg);
            break;
        }
    }
    close(sockfd);
    return 0;
}

//登录函数
int send_login(int sockfd, NETDATA_t *msg)
{
    //组装语句
    bzero(msg, sizeof(NETDATA_t));
    printf("请输入用户名:->");
    fgets(msg->user.name, sizeof(msg->user.name), stdin);
    msg->user.name[strlen(msg->user.name) - 1] = '\0';
    printf("请输入密码:->");
    fgets(msg->user.password, sizeof(msg->user.password), stdin);
    msg->user.password[strlen(msg->user.password) - 1] = '\0';
    // printf("用户名：[%s]密码：[%s]\n", msg->user.name, msg->user.password); //debug

    msg->oper_mod = LOGIN;
    //发送请求
    if (-1 == send(sockfd, msg, sizeof(NETDATA_t), 0))
    {
        ERRLOG("send_login error\n");
    }
    bzero(msg, sizeof(NETDATA_t));

    if (-1 == recv(sockfd, msg, sizeof(NETDATA_t), 0))
    {
        ERRLOG("recv_login error\n");
    }

    if (MY_S == msg->oper_mod) //成功登录
    {
        printf("[%s]已成功登录！！\n", msg->user.name);
        if (1 == msg->aut_man)
        {
            return 2; //管理员登录
        }
        else
        {
            return 1; //普通用户登录
        }
    }
    else if (MY_F == msg->oper_mod) //失败重新输入用户名密码
    {
        printf("[%s]登录失败!!\n", msg->user.name);
    }
    else
    {
        printf("操作码错误[%d]\n", msg->oper_mod);
    }
    return 0;
}

//管理员操作函数
void do_admim(int sockfd, NETDATA_t *msg)
{
    char choose[2];
    while (1)
    {
        admin_checkmenu();
        fgets(choose, sizeof(choose), stdin);
        getchar();
        switch (choose[0])
        {
        case '1':
            //查询员工信息
            send_search(sockfd, msg);
            break;
        case '2':
            //新增员工信息
            send_add(sockfd, msg);
            break;
        case '3':
            //修改员工信息
            send_modify(sockfd, msg);
            break;
        case '4':
            //删除员工信息
            send_del(sockfd, msg);
            break;
        case '5':
            //退出
            do_quit(sockfd, msg);
            return;
            break;
        default:
            break;
        }
    }
}

//普通用户操作函数
void do_user(int sockfd, NETDATA_t *msg)
{
    char choose[2];
    while (1)
    {
        user_checkmenu();
        fgets(choose, sizeof(choose), stdin);
        getchar();
        switch (choose[0])
        {
        case '1':
            //查询本人信息
            send_search(sockfd, msg);
            break;
        case '2':
            //修改密码
            send_modify(sockfd, msg);
            break;
        case '3':
            //退出
            do_quit(sockfd, msg);
            return;
            break;
        default:
            break;
        }
    }
}

//查询函数
void send_search(int sockfd, NETDATA_t *msg)
{
    int aut = 0;
    int flag = 0;
    if (1 == msg->aut_man)
    {
        aut = 1;
        bzero(msg->user.name, sizeof(msg->user.name));
        printf("请输入要查询的用户名:->");
        fgets(msg->user.name, sizeof(msg->user.name), stdin);
        msg->user.name[strlen(msg->user.name) - 1] = '\0';
    }
    msg->oper_mod = FIND;
    if (-1 == send(sockfd, msg, sizeof(NETDATA_t), 0))
        ERRLOG("send_search error");

    if (-1 == recv(sockfd, msg, sizeof(NETDATA_t), 0))
        ERRLOG("recv_search error");

    if (MY_S == msg->oper_mod && msg->aut_man != 2)
    {
        // id|name|password|age|position|tel|addr|salary
        printf("ID:[%d] 姓名:[%s] 年龄:[%d] 职位:[%s] 电话:[%s] 地址:[%s] 薪资:[%.1f]\n",
               msg->info.id, msg->info.name, msg->info.age, msg->info.position, msg->info.tel, msg->info.addr, msg->info.salary);
        flag++;
        if (-1 == recv(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("recv_search error");
    }
    if (0 == flag)
    {
        printf("查询失败,未找到该员工\n");
    }
    msg->aut_man = aut;
    printf("按任意键继续...\n");
    getchar();
}

//修改函数
void send_modify(int sockfd, NETDATA_t *msg)
{
    char buf[20] = {0};
    if (1 == msg->aut_man)
    {
        bzero(msg->user.name, sizeof(msg->user.name));
        printf("请输入要修改的用户名:->");
        fgets(msg->user.name, sizeof(msg->user.name), stdin);
        msg->user.name[strlen(msg->user.name) - 1] = '\0';
    }
    bzero(msg->user.password, sizeof(msg->user.password));
    printf("请输入修改后的密码:->");
    fgets(msg->user.password, sizeof(msg->user.password), stdin);
    msg->user.password[strlen(msg->user.password) - 1] = '\0';
    if (1 == msg->aut_man)
    {
        bzero(buf, sizeof(buf));
        printf("请输入修改后的ID号:->");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        msg->info.id = atoi(buf);

        bzero(buf, sizeof(buf));
        printf("请输入修改后的年龄:->");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        msg->info.age = atoi(buf);

        bzero(msg->info.position, sizeof(msg->info.position));
        printf("请输入修改后的职位:->");
        fgets(msg->info.position, sizeof(msg->info.position), stdin);
        msg->info.position[strlen(msg->info.position) - 1] = '\0';

        bzero(msg->info.tel, sizeof(msg->info.tel));
        printf("请输入修改后的TEL:->");
        fgets(msg->info.tel, sizeof(msg->info.tel), stdin);
        msg->info.tel[strlen(msg->info.tel) - 1] = '\0';

        bzero(msg->info.addr, sizeof(msg->info.addr));
        printf("请输入修改后的地址:->");
        fgets(msg->info.addr, sizeof(msg->info.addr), stdin);
        msg->info.addr[strlen(msg->info.addr) - 1] = '\0';

        bzero(buf, sizeof(buf));
        printf("请输入修改后的薪水:->");
        fgets(buf, sizeof(buf), stdin);
        buf[strlen(buf) - 1] = '\0';
        msg->info.salary = atof(buf); //字符串转double

        printf("ID:[%d] 姓名:[%s] 密码[%s] 年龄:[%d] 职位:[%s] 电话:[%s] 地址:[%s] 薪资:[%.1f]\n",
               msg->info.id, msg->user.name, msg->user.password, msg->info.age, msg->info.position, msg->info.tel, msg->info.addr, msg->info.salary);
    }
    printf("是否确认修改？(y/n)\n");
    bzero(buf, sizeof(buf));
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    if ('y' == buf[0])
    {
        msg->oper_mod = CHANGE;
        if (-1 == send(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("send_modify error");
        if (-1 == recv(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("send_modify error");
        if (MY_S == msg->oper_mod)
        {
            printf("修改已完成！\n");
        }
        else
        {
            printf("修改失败！\n");
        }
    }
    else
    {
        printf("已放弃修改...\n");
    }
    printf("按任意键继续...\n");
    getchar();
}

//添加员工函数
void send_add(int sockfd, NETDATA_t *msg)
{
    char buf[20] = {0};

    bzero(msg->user.name, sizeof(msg->user.name));
    printf("请输入要添加的员工名:->");
    fgets(msg->user.name, sizeof(msg->user.name), stdin);
    msg->user.name[strlen(msg->user.name) - 1] = '\0';

    bzero(msg->user.password, sizeof(msg->user.password));
    printf("请输入的密码:->");
    fgets(msg->user.password, sizeof(msg->user.password), stdin);
    msg->user.password[strlen(msg->user.password) - 1] = '\0';

    bzero(buf, sizeof(buf));
    printf("请输入ID号:->");
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    msg->info.id = atoi(buf);

    bzero(buf, sizeof(buf));
    printf("请输入年龄:->");
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    msg->info.age = atoi(buf);

    bzero(msg->info.position, sizeof(msg->info.position));
    printf("请输入职位:->");
    fgets(msg->info.position, sizeof(msg->info.position), stdin);
    msg->info.position[strlen(msg->info.position) - 1] = '\0';

    bzero(msg->info.tel, sizeof(msg->info.tel));
    printf("请输入TEL:->");
    fgets(msg->info.tel, sizeof(msg->info.tel), stdin);
    msg->info.tel[strlen(msg->info.tel) - 1] = '\0';

    bzero(msg->info.addr, sizeof(msg->info.addr));
    printf("请输入地址:->");
    fgets(msg->info.addr, sizeof(msg->info.addr), stdin);
    msg->info.addr[strlen(msg->info.addr) - 1] = '\0';

    bzero(buf, sizeof(buf));
    printf("请输入薪水:->");
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    msg->info.salary = atof(buf); //字符串转double

    printf("ID:[%d] 姓名:[%s] 密码[%s] 年龄:[%d] 职位:[%s] 电话:[%s] 地址:[%s] 薪资:[%.1f]\n",
           msg->info.id, msg->user.name, msg->user.password, msg->info.age, msg->info.position, msg->info.tel, msg->info.addr, msg->info.salary);

    printf("是否确认添加成员？(y/n)\n");
    bzero(buf, sizeof(buf));
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    if ('y' == buf[0])
    {
        msg->oper_mod = ADD;
        if (-1 == send(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("send_modify error");
        if (-1 == recv(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("send_modify error");
        if (MY_S == msg->oper_mod)
        {
            printf("已完成成员添加！！！\n");
        }
        else
        {
            printf("添加成员失败！\n");
        }
    }
    else
    {
        printf("已放弃添加成员...\n");
    }
    printf("按任意键继续...\n");
    getchar();
}
//删除员工函数
void send_del(int sockfd, NETDATA_t *msg)
{
    char buf[N];
    bzero(msg->user.name, sizeof(msg->user.name));
    printf("请输入要删除的用户名:->");
    fgets(msg->user.name, sizeof(msg->user.name), stdin);
    msg->user.name[strlen(msg->user.name) - 1] = '\0';

    printf("是否确认删除员工[%s]?(y/n)\n",msg->user.name);
    bzero(buf, sizeof(buf));
    fgets(buf, sizeof(buf), stdin);
    buf[strlen(buf) - 1] = '\0';
    if ('y' == buf[0])
    {
        msg->oper_mod = DEL;
        if (-1 == send(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("send_modify error");
        if (-1 == recv(sockfd, msg, sizeof(NETDATA_t), 0))
            ERRLOG("send_modify error");
        if (MY_S == msg->oper_mod)
        {
            printf("已成功删除员工！！！\n");
        }
        else
        {
            printf("删除成员失败！\n");
        }
    }
    else
    {
        printf("已放弃删除员工...\n");
    }
    printf("按任意键继续...\n");
    getchar();
}

//退出操作函数
void do_quit(int sockfd, NETDATA_t *msg)
{
    msg->oper_mod = QUIT; //发送退出信息，告知服务器用户正常退出
    if (-1 == send(sockfd, msg, sizeof(NETDATA_t), 0))
    {
        ERRLOG("send_q error");
    }
}
